
let myStore;
function myRequest(param) {
	let token = myStore.state.userInfo==null?"":myStore.state.userInfo.token;
	if(myStore.state.userInfo!=null){
		if(myStore.state.userInfo.token){
			token = myStore.state.userInfo.token.token;
		}		
	}
	if (token) {
		if (param.header == undefined) {
			param.header = {
				
			};
		}
		param.header.token = token;
	}
	return uni.request(param).then(data => {
		var [err, res] = data;
		if (err != null) {
			console.log(res)
		} else {
			if (res.data.code === 401) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
			} else {
				return res.data
			}
		}
	});
}

export function init(store){
	myStore = store;
}

export function get(url, data) {
	let param = {
		method: 'GET',
		url: url,
		data: data
	};
	return myRequest(param);
}

export function post(url, data) {
	let param = {
		method: 'POST',
		url: url,
		data: data
	};
	return myRequest(param);
}
